package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysSchoolMapper;
import com.ruoyi.system.domain.SysSchool;
import com.ruoyi.system.service.ISysSchoolService;
import com.ruoyi.common.core.text.Convert;

/**
 * 学校Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class SysSchoolServiceImpl implements ISysSchoolService 
{
    @Autowired
    private SysSchoolMapper sysSchoolMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询学校
     * 
     * @param schoolId 学校主键
     * @return 学校
     */
    @Override
    public SysSchool selectSysSchoolBySchoolId(Long schoolId)
    {
        return sysSchoolMapper.selectSysSchoolBySchoolId(schoolId);
    }

    /**
     * 查询学校列表
     * 
     * @param sysSchool 学校
     * @return 学校
     */
    @Override
    public List<SysSchool> selectSysSchoolList(SysSchool sysSchool)
    {
        return sysSchoolMapper.selectSysSchoolList(sysSchool);
    }

    /**
     * 查询学校管理树
     *
     * @param school 学校信息
     * @return 所有学校信息
     */
    @Override
    public List<Ztree> selectSchoolTree(SysSchool school)
    {
        List<SysSchool> schoolList = sysSchoolMapper.selectSysSchoolList(school);
        List<Ztree> ztrees = initZtree(schoolList);
        return ztrees;
    }


    /**
     * 对象转学校树
     *
     * @param schoolList 学校列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<SysSchool> schoolList)
    {
        return initZtree(schoolList, null);
    }

    /**
     * 对象转学校树
     *
     * @param schoolList 学校列表
     * @param roleSchoolList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<SysSchool> schoolList, List<String> roleSchoolList)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = StringUtils.isNotNull(roleSchoolList);
        String schoolName = configService.selectConfigByKey("sys.config.companyName");
        Ztree ztree = new Ztree();
        ztree.setpId(0L);
        ztree.setId(100L);
        ztree.setName(schoolName);
        ztree.setTitle(schoolName);
        ztrees.add(ztree);

        HashMap<String, Long> citys = new HashMap<String, Long>();

        Long cityId = 100L;
        for (SysSchool school : schoolList) {
            if (UserConstants.SCHOOL_NORMAL.equals(school.getStatus())) {
                if(!citys.containsKey(school.getCity())){
                    String city = school.getCity();
                    ztree = new Ztree();
                    ztree.setpId(100L);
                    cityId = cityId + 1;
                    citys.put(city, cityId);
                    ztree.setId(cityId);
                    ztree.setName(city);
                    ztree.setTitle(city);
                    ztrees.add(ztree);
                }
            }
        }
        for (SysSchool school : schoolList)
        {
            if (UserConstants.SCHOOL_NORMAL.equals(school.getStatus()))
            {
                ztree = new Ztree();
                ztree.setId(1000+school.getSchoolId());
                ztree.setpId(citys.get(school.getCity()));
                ztree.setName(school.getSchoolName());
                ztree.setTitle(school.getSchoolName());
                if (isCheck)
                {
                    ztree.setChecked(roleSchoolList.contains(school.getSchoolId() + school.getSchoolName()));
                }
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }


    /**
     * 新增学校
     * 
     * @param sysSchool 学校
     * @return 结果
     */
    @Override
    public int insertSysSchool(SysSchool sysSchool)
    {
        sysSchool.setCreateTime(DateUtils.getNowDate());
        return sysSchoolMapper.insertSysSchool(sysSchool);
    }

    /**
     * 修改学校
     * 
     * @param sysSchool 学校
     * @return 结果
     */
    @Override
    public int updateSysSchool(SysSchool sysSchool)
    {
        sysSchool.setUpdateTime(DateUtils.getNowDate());
        return sysSchoolMapper.updateSysSchool(sysSchool);
    }

    /**
     * 批量删除学校
     * 
     * @param schoolIds 需要删除的学校主键
     * @return 结果
     */
    @Override
    public int deleteSysSchoolBySchoolIds(String schoolIds)
    {
        return sysSchoolMapper.deleteSysSchoolBySchoolIds(Convert.toStrArray(schoolIds));
    }

    /**
     * 删除学校信息
     * 
     * @param schoolId 学校主键
     * @return 结果
     */
    @Override
    public int deleteSysSchoolBySchoolId(Long schoolId)
    {
        return sysSchoolMapper.deleteSysSchoolBySchoolId(schoolId);
    }
}
